package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RoleMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.Role;
import com.heima.pojo.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.dto.UserPageDto;
import com.heima.pojo.dto.UserSearchDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.UserPageVo;
import com.heima.pojo.vo.UserSearchPageVo;
import com.heima.pojo.vo.UserSearchVo;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Beans;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public LoginUserVo login(LoginDto dto) {

        if (dto == null) {
            // 参数异常
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName, dto.getLoginName());

        User user = userMapper.selectOne(wrapper);

        if (user == null) { // 代表用户不存在得

        }
        if (!user.getPassword().equals(dto.getPassword())) { // 代表密码错误

        }
        // 密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId().toString());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    /**
     * 用户列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public UserPageVo pageQuery(UserPageDto dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<User> users = userMapper.pageQuery(dto);
        List<Role> roles = roleMapper.queryAll();
        users.forEach(user -> {
            roles.forEach(role -> {
                if (role.getRoleId() == user.getRoleId()) {
                    user.setRole(role);
                }
            });
        });
        UserPageVo userPageVo = new UserPageVo();
        userPageVo.setTotalPage(Integer.parseInt(users.getPages() + ""));
        userPageVo.setPageSize(dto.getPageSize());
        userPageVo.setPageIndex(dto.getPageIndex());
        userPageVo.setTotalCount(users.getTotal());
        userPageVo.setCurrentPageRecords(users.getResult());
        return userPageVo;
    }

    /**
     * 新增人员
     */
    @Override
    public void save(UserDto dto) {
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        if (dto.getStatus()) {
            user.setStatusValue(1);
        }
        if (user.getId() == null) {
            userMapper.add(user);
            return;
        }
        userMapper.updateUser(user);

    }

    /**
     * 删除人员
     * @param userId
     */
    @Override
    public void delById(String userId) {
        userMapper.delById(userId);
    }

    /**
     * 查询人员详情
     * @param id
     * @return
     */
    @Override
    public User queryById(String id) {
        return userMapper.queryById(Integer.parseInt(id));
    }

    /**
     * 人员工作量列表
     */
    @Override
    public UserSearchPageVo userSearch(UserSearchDto dto) {
        dto.check();
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<UserSearchVo> page = userMapper.userSearch(dto);
        log.info("mapper中拿到的数据为:{}",page);
        UserSearchPageVo userSearchPageVo = new UserSearchPageVo();
        userSearchPageVo.setCurrentPageRecords(page);
        userSearchPageVo.setPageIndex(dto.getPageIndex());
        userSearchPageVo.setPageSize(dto.getPageSize());
        userSearchPageVo.setTotalPage(page.getPages());
        userSearchPageVo.setTotalCount(page.getTotal());
        return userSearchPageVo;
    }

    /**
     * 工单统计
     * @param start
     * @param end
     * @param userId
     * @return
     */
    @Override
    public List<UserSearchVo> userWork(Date start, Date end, String userId) {
        return userMapper.userWork(start,end,userId);
    }

    /**
     * 根据售货机获取运营人员列表
     */
    @Override
    public List<UserSearchVo> queryListByInnerCode(String innerCode) {
        return userMapper.queryListByInnerCode(innerCode);
    }

}
